<? 
require("adm_header.php"); 
writeCommonAdminTable(LA_ADMIN_ADS_ADMINISTRATION,1);
$tplPackages = new tplLoad();

$lLimit=15;
$lStop=0;
if (getParam("make_special","") && WA())
{
	if (getParam("make_special","") == 1)
		q("update $ads_tbl set ad_is_special = 1 where ad_id = ". getParam("edit_id",""));
	else if (getParam("make_special","") == 2) 
		q("update $ads_tbl set ad_is_special = 0 where ad_id = ". getParam("edit_id",""));
}  

if (getParam("make_premium","") && WA())
{
	if (getParam("make_premium","") == 1)
		q("update $ads_tbl set ad_is_premium = 1 where ad_id = ". getParam("edit_id",""));
	else if (getParam("make_premium","") == 2) 
		q("update $ads_tbl set ad_is_premium = 0 where ad_id = ". getParam("edit_id",""));
} 


if (getParam("validated","")==2)
{
	if ($set_validation!=1)
	{
		$tplPackages->assign("msg","<p><b class='warning'>".LA_ADMIN_AD_VAL_NOT_ACTIVATED."</b><br />".LA_ADMIN_AD_YOU_MAY . " <a href='adm_set.php?file=opt_inc.php#set_validation'>".LA_ADMIN_AD_ACTIVATE_IT."</a>.</p>");
		$lStop=1;
		$tplPackages->assign("hide",1);
	}
	else 
		$tplPackages->assign("msg","<p><b>".LA_ADMIN_NOT_VALIDATED_ADS."</b></p>");
}
elseif (getParam("ads_by_cat",""))
{
	$sql = "select cat_name from $cat_tbl where cat_id=" . getParam("ads_by_cat","");
	$re = mysql_query($sql);
	$row = mysql_fetch_array($re);
	$cat_name = $row["cat_name"];
	$tplPackages->assign("msg",LA_ADMIN_NOW_SHOWING_ADS_IN_CATEGORY . " <b>$cat_name</b>");
}
elseif (getParam("ads_by_user",""))
{
	$sql = "select user_name from $usr_tbl where user_id=" . getParam("ads_by_user","");
	$re = mysql_query($sql);
	$row = mysql_fetch_array($re);
	$user_name = $row["user_name"];
	$tplPackages->assign("msg",LA_ADMIN_NOW_SHOWING_ADS_TO_USER . " <b>$user_name</b>");
}


if (getParam("activate","") && WA())
{
	$lTitle=getParam("title","");
	q("update $ads_tbl set ad_is_validated=1 where ad_id=" . getParamInt("activate",""));
	if ($set_inform_user_ad_valid)
	{
		$sql="select user_email,user_name,ad_title,ad_id from $usr_tbl,$ads_tbl where ad_owner=user_id AND ad_id='" . getParamInt("activate","") . "'";
		$r=q($sql);
		$row=mysql_fetch_array($r);
		$lTitle=$row["ad_title"];
		$lEmail=$row["user_email"];
		$lad_id=$row["ad_id"];
		$lName=$row["user_name"];
		
		if ($lTitle && $lEmail && $lad_id)
		{
			$lNewAdArray=array("$lName","$lEmail","","$set_sitename","","http://$set_url/member_login.php","http://$set_url/" . linkDetail($lad_id),"$lTitle");
			
			$lSubjectText=getLangString($set_language,"LA_MAIL_APPROVE_AD_SUBJECT");
			$lBodyText=getLangString($set_language,"LA_MAIL_APPROVE_AD_BODY");
			
			$lSubject = formatString($lSubjectText, $lNewAdArray);
			$lBody = formatString($lBodyText, $lNewAdArray);
			
			sendEmail($lEmail,$set_webmaster_address, $lSubject, $lBody);
		}
			
		
	}
	$tplPackages->assign("msg",formatString(LA_ADMIN_TITLE_APPROVED,array("<strong>$lTitle</strong>")));
	
}


$tplPackages->assign("find",getParam("find",""));
$tplPackages->assign("catlist",getCategoriesDD(getParam("ad_cat_id",""),0));

if (getParam("delete","")>0)
{
	if (getParam("confirmed","")==1 && WA())
	{
		//delete_ad(round(getParam("deleteid","")));
		//$tplList->assign("ad_deleted",1);
		delete_ad(getParam("delete",""));	
		$tplPackages->assign("msg","<p><b>Information:</b> Ad ".getParam("edit_title","")." with pictures/videos was successfully removed.</p>&nbsp<br>");
	}
	else 
	{
		$lMsg= "<b class='red'>Please confirm deletion of ".getParam("edit_title","")."</b>";
		$lMsg.="<br/><br /><a href='adm_classifieds_list.php?delete=".getParam("delete","")."&confirmed=1&ad_title=".getParam("edit_title","").getAllParams(array("delete")) . "'><b><u>Yes, delete it</u></b></a>";
		$tplPackages->assign("msg",$lMsg);
	}
	
	
	
}

// Find total
$lFind=getParam("find","");
$lSearchLimiter="";
$lFind=strToDb($lFind);

if (getParam("ad_cat_id",""))
	$lSearchLimiter.=" AND ad_cat_id=" . getParam("ad_cat_id","");
	
if (getParam("find",""))
	$lSearchLimiter.=" AND (ad_id='$lFind' OR ad_title like '%$lFind%' OR ad_description like '%$lFind%' OR cat_name like '%$lFind%' OR ad_owner like '%$lFind%')";	

if (getParam("ads_by_user",""))
	$lSearchLimiter.=" AND (ad_owner = '". getParam("ads_by_user","") . "')";

if (getParam("ads_by_cat",""))
	$lSearchLimiter.=" AND (ad_cat_id = '". getParam("ads_by_cat","") . "')";
	
if (getParam("ad_id_show",""))
	$lSearchLimiter.=" AND (ad_id = '". getParam("ad_id_show","") . "')";

if (getParam("validated","")==2)
	$lSearchLimiter.=" AND (ad_is_validated=0)";	
	

// Get total count
$sql_select = "select count(ad_id) as num_rows from $ads_tbl, $cat_tbl,$usr_tbl where cat_id=ad_cat_id  AND user_id=ad_owner $lSearchLimiter";  
$num_res=q($sql_select);

$lNumAds_row = mysql_fetch_array($num_res);
$lNumAds=$lNumAds_row["num_rows"];
$tplPackages->assign("url_premium",getAllParams(array("make_premium","edit_id","make_special")));
$tplPackages->assign("url_special",getAllParams(array("make_special","edit_id","make_premium")));
$tplPackages->assign("url_delete",getAllParams(array("delete","confirmed")));
$tplPackages->assign("url_heading",getAllParams(array("orderby","orderway")));
$retArray=urlBar($lNumAds,$lLimit,getParam("page",1),getAllParams(array("page")));

$sql_limit=$retArray["sql_limit"];

if ($lStop==0)
{
	$tplPackages->assign("num_ads",$lNumAds);
	$sql_select = "select user_id,ad_owner,ad_is_validated,user_name,ad_id,ad_title,ad_date,ad_description,ad_is_special,ad_is_premium,ad_date,ad_cat_id,ad_views,ad_votes, cat_id, cat_name from $ads_tbl, $cat_tbl,$usr_tbl where cat_id=ad_cat_id AND user_id=ad_owner $lSearchLimiter order by " . getParam("orderby","ad_id") . "  " .getParam("orderway","desc") . " $sql_limit";  
	$result = q ($sql_select);
	
	
	$set_special=1;
	$i=0;
	
	while ($row = mysql_fetch_array($result))
	{
		
		$ad_id = $row["ad_id"];
		$ad_owner = $row["ad_owner"];
		$ad_title = $row["ad_title"];
		$ad_description = $row["ad_description"];
		$ad_date = $row["ad_date"];
		$user_name = $row["user_name"];
		$user_id = $row["user_id"];
		$ad_views = $row["ad_views"];
		$ad_votes = $row["ad_votes"];
		$cat_id = $row["cat_id"];
		$cat_name = $row["cat_name"];
		$ad_is_premium = $row["ad_is_premium"];
		$ad_is_validated = $row["ad_is_validated"];
		$ad_is_special = $row["ad_is_special"];
		$color=colorSwitch($color);
		$lAdsArray[$i]["ad_id"]=$ad_id;
		$lAdsArray[$i]["ad_owner"]=$ad_owner;
		$lAdsArray[$i]["ad_date"]=formatDateShort($ad_date);
		$lAdsArray[$i]["ad_title"]=strEnc(dotString($ad_title,20));
		$lAdsArray[$i]["cat_id"]=$cat_id;
		$lAdsArray[$i]["cat_name"]=strEnc(dotString($cat_name,15));
		$lAdsArray[$i]["ad_description"]=strEnc($ad_description);
		$lAdsArray[$i]["user_name"]=strEnc(dotString($user_name,10));
		$lAdsArray[$i]["user_id"]=$user_id;
		$lAdsArray[$i]["ad_is_validated"]=$ad_is_validated;
		$lAdsArray[$i]["ad_is_special"]=$ad_is_special;
		$lAdsArray[$i]["ad_is_premium"]=$ad_is_premium;
		$lAdsArray[$i]["color"]=$color;
		$lAdsArray[$i]["NB"]=$i;
	    $i++;
	    
	}
	
	$tplPackages->assign("url_bar",$retArray["url_bar"]);
} // If $lStop==0;


if ($set_validation) // Ad Validation is On
	$tplPackages->assign("set_ad_validation",1);
	
$tplPackages->assign("listArray",$lAdsArray);
$tplPackages->display("adm_classifieds_list.tpl");
writeCommonAdminTable("","");
require("adm_footer.php"); 
?>